<template>
  <el-dialog title="新增工作模式" :visible.sync="dialogVisible">
    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
      <el-tab-pane label="礼拜制" name="weekly">
        <!-- 礼拜制内容 -->
        <el-form :model="formData" ref="form">
          <el-form-item label="工作模式名称" prop="name"
                        :rules="[{ required: true, message: '请输入工作模式名称', trigger: 'blur' }]"
          >
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="连续工作天数" prop="workingDays"
                        :rules="[{ required: true, message: '请选择连续工作天数', trigger: 'change' }]"
          >
            <el-checkbox-group v-model="formData.workingDays">
              <el-checkbox label="monday" value="1">周一</el-checkbox>
              <el-checkbox label="tuesday" value="2">周二</el-checkbox>
              <el-checkbox label="wednesday" value="3">周三</el-checkbox>
              <el-checkbox label="thursday" value="4">周四</el-checkbox>
              <el-checkbox label="friday" value="5">周五</el-checkbox>
              <el-checkbox label="saturday" value="6">周六</el-checkbox>
              <el-checkbox label="sunday" value="7">周日</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="工作时间" required>
            <el-time-picker value-format="yyyy-MM-dd" v-model="formData.startTime" placeholder="开始时间"></el-time-picker>
            <span style="margin: 0 10px;">至</span>
            <el-time-picker value-format="yyyy-MM-dd" v-model="formData.endTime" placeholder="结束时间"></el-time-picker>
          </el-form-item>
          <el-form-item>
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm">确定</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="连续制" name="continuous">
        <!-- 连续制内容 -->
        <!-- 根据需求添加连续制的表单内容 -->
        <el-form :model="formData" ref="form">
          <el-form-item label="工作模式名称" prop="name"
                        :rules="[{ required: true, message: '请输入工作模式名称', trigger: 'blur' }]"
          >
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="连续工作天数" prop="workingDays"
                        :rules="[{ required: true, message: '请选择连续工作天数', trigger: 'change' }]"
          >
            上<el-input v-model="formData.workingDays" width="10px" style="display: inline-block"></el-input>
            休<el-input v-model="formData.workingDays" width="10px" style="display: inline-block"></el-input>
          </el-form-item>
          <el-form-item label="工作时间" required>
            <el-time-picker value-format="yyyy-MM-dd" v-model="formData.startTime" placeholder="开始时间"></el-time-picker>
            <span style="margin: 0 10px;">至</span>
            <el-time-picker value-format="yyyy-MM-dd" v-model="formData.endTime" placeholder="结束时间"></el-time-picker>
          </el-form-item>
          <el-form-item>
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm">确定</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import { addWorkPattern } from '@/api/vehicle'

export default {
  name: 'WorkArrangeSettingAdd',
  data() {
    return {
      dialogVisible: false,
      activeTab: 'weekly', // 默认激活的Tab
      formData: {
        workingDays: [],
        friday: '2',
        monday: '2',
        name: '',
        operator: null,
        restDayNum: 0,
        saturday: '2',
        sunday: '2',
        thursday: '2',
        tuesday: '2',
        userType: '3',
        wednesday: '2',
        workDayNum: null,
        workEndMinute1: null,
        workPatternType: '1',
        workStartMinute1: null
      }
    }
  },
  methods: {
    initFormData(data) {
      this.formData = data
      console.log(this.formData, 333)
      this.setWorkingDays()
      console.log(this.formData.workingDays, 2222)
    },
    setWorkingDays() {
      const workingDays = []
      if (this.formData.monday === 1) {
        workingDays.push('monday')
      }
      if (this.formData.tuesday === 1) {
        workingDays.push('tuesday')
      }
      if (this.formData.wednesday === 1) {
        workingDays.push('wednesday')
      }
      if (this.formData.thursday === 1) {
        workingDays.push('thursday')
      }
      if (this.formData.friday === 1) {
        workingDays.push('friday')
      }
      if (this.formData.saturday === 1) {
        workingDays.push('saturday')
      }
      if (this.formData.sunday === 1) {
        workingDays.push('sunday')
      }
      this.formData.workingDays = workingDays
    },
    handleTabClick(tab) {
      // 处理tab切换时的逻辑
    },
    showDialog() {
      this.dialogVisible = true
    },
    hideDialog() {
      this.dialogVisible = false
    },
    submitForm() {
      this.formData.workingDays.forEach(day => {
        this.formData[day] = '1'
      })

      // 计算休息天数
      const totalDays = 7 // 一周总共7天
      const workDays = this.formData.workingDays.length
      this.formData.restDayNum = totalDays - workDays
      this.formData.workDayNum = workDays

      // 表单验证和提交逻辑
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 提交表单数据
          console.log('表单数据', this.formData)
          addWorkPattern(this.formData)
            .then(res => {
              if (res.code === 200) {
                this.$message.success('新增成功')
                this.hideDialog()
                this.$emit('refresh')
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch(err => {
              this.$message.error('新增失败')
              console.log('提交失败', err)
            })
        } else {
          console.log('表单验证失败')
        }
      })
    }
  }
}
</script>
